package org.bees.cms.web.util.velocity.editer;

import java.io.Serializable;

import net.sf.json.JSONObject;

import org.bees.cms.web.util.Beans;

public abstract class AbstractEditer implements Editer {
	protected String id = "";
	protected String clazz = "";
	protected String style = "";
	protected String name;
	protected String value = "";// 缺省值
	protected String property;
	protected String placeholder = "";
	protected JSONObject validator = new JSONObject();

	@Override
	public void initialize(JSONObject json) throws Exception {
		id = json == null ? id : json.containsKey("id") ? json.getString("id") : id;
		clazz = json == null ? clazz : json.containsKey("clazz") ? json.getString("clazz") : clazz;
		style = json == null ? style : json.containsKey("style") ? json.getString("style") : style;
		name = json == null ? name : json.containsKey("name") ? json.getString("name") : name;
		value = json == null ? value : json.containsKey("value") ? json.getString("value") : value;
		property = json == null ? property : json.containsKey("property") ? json.getString("property") : property;
		placeholder = json == null ? placeholder : json.containsKey("placeholder") ? json.getString("placeholder") : placeholder;
		validator = json == null ? validator : json.containsKey("validator") ? json.getJSONObject("validator") : validator;
	}

	@Override
	public String edit(Serializable t) throws Exception {
		value = t == null ? value : Beans.getString(t, property);
		StringBuilder builder = new StringBuilder();
		builder.append("<input");
		builder.append(" id='" + id + "'");
		builder.append(" class='" + clazz + "'");
		builder.append(" style='" + style + "'");
		builder.append(" type='text'");
		builder.append(" name='" + name + "'");
		builder.append(" value='" + value + "'");
		builder.append(" placeholder='" + placeholder + "'");
		builder.append(" validator='" + validator + "'");
		builder.append(" />");
		return builder.toString();
	}

	@Override
	public void destroy() throws Exception {
		id = null;
		clazz = null;
		style = null;
		name = null;
		value = null;
		property = null;
		placeholder = null;
		validator = null;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getPlaceholder() {
		return placeholder;
	}

	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}

	public JSONObject getValidator() {
		return validator;
	}

	public void setValidator(JSONObject validator) {
		this.validator = validator;
	}

}
